package com.lagou.edu.boss.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lagou.edu.boss.entity.bo.UpLoadResult;
import com.lagou.edu.boss.entity.form.CourseForm;
import com.lagou.edu.boss.entity.vo.CourseVo;
import com.lagou.edu.boss.service.impl.OssService;
import com.lagou.edu.common.entity.vo.Result;
import com.lagou.edu.course.api.CourseRemoteService;
import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * Author:   lg
 * Date:
 * Description: 课程
 */
@Api(tags = "课程", produces = "application/json")
@Slf4j
@Controller
@RequestMapping("/course/")
public class CourseController {

    @Autowired
    private CourseRemoteService courseRemoteService;
    @Autowired
    private OssService ossService;

    @ApiOperation(value = "保存或者更新课程信息")
    @PostMapping("saveOrUpdateCourse")
    @ResponseBody
    public Result saveOrUpdateCourse(@RequestBody CourseForm courseForm) {
        CourseDTO courseDTO = new CourseDTO();
        BeanUtils.copyProperties(courseForm, courseDTO);
        return Result.success(courseRemoteService.saveOrUpdateCourse(courseDTO));
    }

    /**
     *
     */
    @ApiOperation(value = "课程上下架")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "courseId", value = "课程ID"),
            @ApiImplicitParam(name = "status", value = "课程状态，0-草稿，1-上架")
    })
    @PutMapping("changeState")
    @ResponseBody
    public Result changeState(@RequestParam("courseId") Integer courseId,
                              @RequestParam("status") Integer status) {

        boolean result = courseRemoteService.changeState(courseId, status);
        return Result.success(result);
    }

    @ApiOperation(value = "分页查询课程信息")
    @PostMapping("getQueryCourses")
    @ResponseBody
    public Result getQueryCourses(@RequestBody CourseQueryParam courseQueryParam) {
        Integer currentPage = courseQueryParam.getCurrentPage();
        Integer pageSize = courseQueryParam.getPageSize();
        if (null == currentPage || currentPage <= 0) {
            courseQueryParam.setCurrentPage(1);
        }
        if (null == pageSize || pageSize <= 0) {
            courseQueryParam.setPageSize(10);
        }

        try {
            Page<CourseDTO> queryCoursePage = courseRemoteService.getQueryCourses(courseQueryParam);
            Page<CourseVo> courseVoPage = new Page<>();
            BeanUtils.copyProperties(queryCoursePage, courseVoPage);
            return Result.success(courseVoPage);
        } catch (Exception e) {
            log.error("分页查询课程信息:", e);
            return Result.fail(e.getMessage());
        }
    }


    @ApiOperation(value = "上传图片")
    @PostMapping("/upload")
    @ResponseBody
    public Result upload(@RequestParam("file") MultipartFile multipartFile) {
        UpLoadResult upLoadResult = ossService.upload(multipartFile);
        return Result.success(upLoadResult);
    }

}
